import Vue from "vue";
import VueRouter from "vue-router";
import Login from "../views/Login.vue";
import { checktoken } from "@/apis/user.js";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "Login",
    component: Login,
  },
  {
    path: "/home",
    name: "Home",
    component: () => import("../views/Home.vue"),
    children: [
      {
        path: "/home/homemain",
        name: "/home/homemain",
        component: () => import("../views/homemodules/HomeMain.vue"),
      },
      {
        path: "/home/admin",
        name: "/home/admin",
        component: () => import("../views/homemodules/Admin.vue"),
      },
      {
        path: "/home/ordermanange",
        name: "/home/ordermanange",
        component: () => import("../views/homemodules/OrderManange.vue"),
      },
      {
        path: "/home/prolist",
        name: "/home/prolist",
        component: () => import("../views/homemodules/promanange/ProList.vue"),
      },
      {
        path: "/home/proclass",
        name: "/home/proclass",
        component: () => import("../views/homemodules/promanange/ProClass.vue"),
      },
      {
        path: "/home/storemanange",
        name: "/home/storemanange",
        component: () => import("../views/homemodules/StoreManange.vue"),
      },
      {
        path: "/home/addpro",
        name: "/home/addpro",
        component: () => import("../views/homemodules/promanange/AddPro.vue"),
      },
      {
        path: "/home/accoutlist",
        name: "/home/accoutlist",
        component: () => import("../views/homemodules/accout/AccoutList.vue"),
      },
      {
        path: "/home/addaccout",
        name: "/home/addaccout",
        component: () => import("../views/homemodules/accout/AddAccout.vue"),
      },
      {
        path: "/home/editaccout",
        name: "/home/editaccout",
        component: () => import("../views/homemodules/accout/EditAccout.vue"),
      },
      {
        path: "/home/prostatistics",
        name: "/home/prostatistics",
        component: () =>
          import("../views/homemodules/statistics/ProStatistics.vue"),
      },
      {
        path: "/home/orderstatistics",
        name: "/home/orderstatistics",
        component: () =>
          import("../views/homemodules/statistics/OrderStatistics.vue"),
      },
    ],
  },
  {
    path: "*",
    component: () => import("../views/Fail.vue"),
  },
];

const router = new VueRouter({
  routes,
});
router.beforeEach((to, from, next) => {
  if (to.path != "/") {
    checktoken({ token: localStorage.token }).then((res) => {
      if (res.data.code == 0) {
        next();
      } else {
        next("/");
      }
    });
  } else {
    next();
  }
});

export default router;
